package com.temp.demo.jni;

import java.net.URLDecoder;
import java.util.Random;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TempDemoJniActivity extends Activity {
	static {
		System.loadLibrary("Jni");
	}
	private TextView tvEntity;
	private Button bnCustomPermission, bnQuery, bnInsert, bnDelete, bnQueryFirst;
	
	private Uri uriEntity;
	private ContentResolver resolver;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tvEntity = (TextView)findViewById(R.id.textView1);
        
        Jni jni = new Jni();
//        tvEntity.setText(jni.getString()+jni.add());
        String tempString = URLDecoder.decode("%5B%7B%22detailId%22%3A%2210000160%22%2C%22detailName%22%3A%22%E9%A1%B9%E7%9B%AE1%22%2C%22detailPicStr%22%3A%22%22%2C%22detailVal%22%3A%22true%22%7D%5D");
        tvEntity.setText(tempString);
        uriEntity = Uri.parse("content://com.temptest.pushingactionbar/entity");
		resolver = getContentResolver();
		
		
		bnCustomPermission = (Button)findViewById(R.id.button1);
		bnQuery = (Button)findViewById(R.id.button_query);
		bnInsert = (Button)findViewById(R.id.button_insert);
		bnDelete = (Button)findViewById(R.id.button_delete);
		bnQueryFirst = (Button)findViewById(R.id.button_queryfirst);
		bnCustomPermission.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse("temptest://hahahaha"));
				startActivity(intent);
			}
		});
		bnInsert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tvEntity.setText(insertEntity());
			}
		});
		bnQuery.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tvEntity.setText(queryEntity(false));
			}
		});
		bnDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tvEntity.setText(deleteEntity(false));
			}
		});
		bnQueryFirst.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				tvEntity.setText(queryEntity(true));
			}
		});
	}
	
	private static final String[] COLUMNS = {
		"entitytype",
		"entityvalue"
	};
	
	private String queryEntity(boolean queryFirst){
		String retval = "no entitys";
		StringBuilder retvalBuilder = new StringBuilder();
		Cursor cursor;
		if (queryFirst) {
			Uri uriQueryFirst = ContentUris.withAppendedId(uriEntity, 38);
			cursor = resolver.query(uriQueryFirst, COLUMNS, null, null, null);
		}else{
			String selection = "_id<?";
			String[] selectionArgs = new String[]{"100"};
			cursor = resolver.query(uriEntity, COLUMNS, selection, selectionArgs, null);
		}
		
		if (cursor != null) {
			while(cursor.moveToNext()){
				retvalBuilder.append("entitytype:")
				.append(cursor.getString(0))
				.append("entityvalue:")
				.append(cursor.getString(1))
				.append("\n");
			}
			cursor.close();
		}
		retval = TextUtils.isEmpty(retvalBuilder.toString()) ? retval : retvalBuilder.toString();
		return retval;
	}
	
	private String insertEntity(){
		Random random = new Random();
		int index = random.nextInt(10);
		String retval = "insert failed";
		ContentValues values = new ContentValues();
		values.put("entitytype", "type" + index);
		values.put("entityvalue", "value" + index);
		retval = resolver.insert(uriEntity, values).toString();
		return retval;
	}
	
	private String deleteEntity(boolean deleteFirst){
		String retval = "delete count:0";
		int count = 0;
		Uri uriDeleteFirst = ContentUris.withAppendedId(uriEntity, 0);
		if (deleteFirst) {
			count = resolver.delete(uriDeleteFirst, null, null);
		}else{
			count = resolver.delete(uriEntity, null, null);
		}
		retval = "delete count:" + count;
		return retval;
	}
}